home *** CD-ROM | disk | FTP | other *** search
- Path: amaryllisp1.appsig.com!user
- From: larry_kearney@appsig.com (Lawrence Kearney)
- Newsgroups: comp.lang.c
- Subject: Re: HELP!! BAMBOOZLED BEGINNER!!
- Date: Wed, 31 Jan 1996 07:16:04 -0700
- Organization: Applied Signal Technology
- Message-ID: <larry_kearney-3101960716040001@amaryllisp1.appsig.com>
- References: <Pine.OSF.3.91l.960130235948.20497A-100000@saul3.u.washington.edu>
- NNTP-Posting-Host: amaryllisp1.appsig.com
-
- > Hello everybody!
- >
- > I'm a beginning C-programmer and for the past couple of days, I've been
- > totally stuck in my program. The purpose of my program is to ask the user
- > for an integer 'i' and raise it by a power 'n'. I've been succesful in
- > computing 5 to the 6th power, 3 to the 4th power, and other small-sized
- > equations. However, when I try to raise 5 to the 7th power, I get "12589"
- > when it really should be "78125". My teacher told me that instead of using
- > plain 'int', I should use 'long int'. I tried that, but it's still not
- > working. If anyone can help me out here, I'd GREATLY appreciate it! I've
- > included my code below... (by the way, I'm not allowed to use the <math.h>
- > library)
- >
- >
- > <example code deleted>
-
- Two problems immediately come to mind.
-
- Number 1: You have i_total defined as an 'int'. On many CPUs (yours
- included, it seems), an int is 16 bits long. This means it can hold a
- number that ranges from -32768 to 32767. The result of 5 to the 7th is
- obviously larger than this. To correct this problem, define i_total as a
- long or as your teacher suggested, a long int. They're the same thing.
-
- Number 2: In your printf statement, you use %d to format and print the
- result. This is fine if you are printing out an int. If you are trying to
- print a long, it causes problems by displaying only the lower 16 bits of
- the value. To fix this, change it to %ld. This will tell printf that the
- value to be printed is a long it so that it knows to fetch this type of
- integer off the calling stack.
-
- --
- Larry Kearney | "You want fries with that?"
- Applied Signal Technology |
- larry_kearney@appsig.com |
-